﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using ProtoType.DataStorage;
using System.Collections.ObjectModel;
using System.Windows.Markup;
using System.Text;
using AmCharts.Windows.QuickCharts;
using ProtoType.ProtoTypeService;
using ProtoType.GeocodeService;
using System.IO;

namespace ProtoType
{
    public partial class FormData : PhoneApplicationPage
    {
        AppSettings apps = new AppSettings();
        ProtoTypeServiceClient client;
        DataStructureStore dss = new DataStructureStore();
        DataTemplate datatemp = new DataTemplate();
        SerialChart serialChart = new SerialChart();
        Microsoft.Phone.Controls.Maps.Map MyMap = new Microsoft.Phone.Controls.Maps.Map();
        FieldProperties fp;
        ObservableCollection<FieldProperties> itemlist;
        ObservableCollection<XElement> controls = new ObservableCollection<XElement>();
        ObservableCollection<XElement> formitems = new ObservableCollection<XElement>();

        int i = 1;
        string selectedfid = "";
        string LayoutType = "";
        string chartcatbidvalue = "";
        string chartbidvalue = "";
        string mapbidvalue = "";
        string Imgbidvalue = "";
        bool newPageInstance = false;

        public FormData()
        {
            InitializeComponent();
            newPageInstance = true;
            client = new ProtoTypeServiceClient();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (newPageInstance)
            {
                if (NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
                {
                    if (apps.IsolatedStorageSavingSetting)
                    {
                        LoadLayoutControls(dss.LoadlayoutControls(selectedfid));
                        controls = dss.LoadformControlList(selectedfid);
                        formitems = dss.GetSelectedFormItemDetails(selectedfid);
                        GetItemslist();
                    }
                    if (apps.WebServiceSavingSetting)
                    {
                        client.LoadlayoutControlsAsync(selectedfid);
                        client.LoadlayoutControlsCompleted += new EventHandler<LoadlayoutControlsCompletedEventArgs>(client_LoadlayoutControlsCompleted);
                    }
                }
            }
            newPageInstance = false;
            lstData.SelectedIndex = -1;
        }

        void LoadLayoutControls(ObservableCollection<XElement> formelements)
        {
            foreach (XElement ele in formelements)
            {
                PageTitle.Text = ele.Element("name").Value;
                if (ele.Element("layout") != null)
                {
                    LayoutType = ele.Element("layout").Attribute("type").Value;
                    if (LayoutType == "Advanced" && (ele.Element("layout").Element("Imagecontrol") != null))
                    {
                        Imgbidvalue = ele.Element("layout").Element("Imagecontrol").Element("bid").Value;
                    }
                    if (LayoutType == "Map" && (ele.Element("layout").Element("Addresscontrol") != null))
                        mapbidvalue = ele.Element("layout").Element("Addresscontrol").Element("bid").Value;
                    if (LayoutType == "Chart" && (ele.Element("layout").Element("Titlecontrol") != null) && (ele.Element("layout").Element("Numbercontrol") != null))
                    {
                        chartcatbidvalue = ele.Element("layout").Element("Titlecontrol").Element("bid").Value;
                        chartbidvalue = ele.Element("layout").Element("Numbercontrol").Element("bid").Value;
                    }
                }
            }
        }

        void client_LoadlayoutControlsCompleted(object sender, LoadlayoutControlsCompletedEventArgs e)
        {
            LoadLayoutControls(e.Result);
            client.LoadformControlListAsync(selectedfid);
            client.LoadformControlListCompleted += new EventHandler<LoadformControlListCompletedEventArgs>(client_LoadformControlListCompleted);
        }

        void client_LoadformControlListCompleted(object sender, LoadformControlListCompletedEventArgs e)
        {
            controls = e.Result;
            client.GetSelectedFormItemDetailsAsync(selectedfid);
            client.GetSelectedFormItemDetailsCompleted += new EventHandler<GetSelectedFormItemDetailsCompletedEventArgs>(client_GetSelectedFormItemDetailsCompleted);
        }

        void client_GetSelectedFormItemDetailsCompleted(object sender, GetSelectedFormItemDetailsCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                formitems = e.Result;
                GetItemslist();
            }
        }

        private void lstData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index;
            if (lstData.SelectedIndex != -1)
            {
                index = lstData.SelectedIndex;
                index = index + 1;
                NavigationService.Navigate(new Uri("/ItemDetails.xaml?fid=" + selectedfid + "&itemid=" + index, UriKind.Relative));
            }
        }

        void GetItemslist()
        {
            itemlist = new ObservableCollection<FieldProperties>();
            foreach (var item in formitems)
            {
                fp = new FieldProperties();
                foreach (XElement control in controls.Elements("control"))
                {
                    int binding = int.Parse(control.Attribute("id").Value);
                    string cname = control.Element("cname").Value;
                    cname = cname.Replace(" ", "");
                    string ctype = control.Element("ctype").Value;
                    if (item.Element(cname) != null)
                    {
                        switch (binding)
                        {
                            case 1:
                                if ("P1" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P1" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P1" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P1 = item.Element(cname).Value;
                                }
                                break;
                            case 2:
                                if ("P2" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P2" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P2" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P2 = item.Element(cname).Value;
                                }
                                break;
                            case 3:
                                if ("P3" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P3" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P3" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P3 = item.Element(cname).Value;
                                }
                                break;
                            case 4:
                                if ("P4" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P4" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P4" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P4 = item.Element(cname).Value;
                                }
                                break;
                            case 5:
                                if ("P5" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P5" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P5" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P5 = item.Element(cname).Value;
                                }
                                break;
                            case 6:
                                if ("P6" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P6" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P6" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P6 = item.Element(cname).Value;
                                }
                                break;
                            case 7:
                                if ("P7" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P7" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P7" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P7 = item.Element(cname).Value;
                                }
                                break;
                            case 8:
                                if ("P8" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P8" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P8" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P8 = item.Element(cname).Value;
                                }
                                break;
                            case 9:
                                if ("P9" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P9" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P9" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P9 = item.Element(cname).Value;
                                }
                                break;
                            case 10:
                                if ("P10" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P10" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P10" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P10 = item.Element(cname).Value;
                                }
                                break;
                            case 11:
                                if ("P11" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P11" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P11" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P11 = item.Element(cname).Value;
                                }
                                break;
                            case 12:
                                if ("P12" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P12" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P12" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P12 = item.Element(cname).Value;
                                }
                                break;
                            case 13:
                                if ("P13" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P13" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P13" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P13 = item.Element(cname).Value;
                                }
                                break;
                            case 14:
                                if ("P14" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P14" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P14" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P14 = item.Element(cname).Value;
                                }
                                break;
                            case 15:
                                if ("P15" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P15" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P15" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P15 = item.Element(cname).Value;
                                }
                                break;
                            case 16:
                                if ("P16" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P16" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P16" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P16 = item.Element(cname).Value;
                                }
                                break;
                            case 17:
                                if ("P17" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P17" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P17" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P17 = item.Element(cname).Value;
                                }
                                break;
                            case 18:
                                if ("P18" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P18" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P18" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P18 = item.Element(cname).Value;
                                }
                                break;
                            case 19:
                                if ("P19" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P19" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P19" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P19 = item.Element(cname).Value;
                                }
                                break;
                            case 20:
                                if ("P20" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P20" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P20" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P20 = item.Element(cname).Value;
                                }
                                break;
                            case 21:
                                if ("P21" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P21" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P21" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P21 = item.Element(cname).Value;
                                }
                                break;
                            case 22:
                                if ("P22" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P22" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P22" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P22 = item.Element(cname).Value;
                                }
                                break;
                            case 23:
                                if ("P23" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P23" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P23" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P23 = item.Element(cname).Value;
                                }
                                break;
                            case 24:
                                if ("P24" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P24" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P24" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P24 = item.Element(cname).Value;
                                }
                                break;
                            case 25:
                                if ("P25" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P25" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P25" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P25 = item.Element(cname).Value;
                                }
                                break;
                            case 26:
                                if ("P26" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P26" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P26" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P26 = item.Element(cname).Value;
                                }
                                break;
                            case 27:
                                if ("P27" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P27" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P27" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P27 = item.Element(cname).Value;
                                }
                                break;
                            case 28:
                                if ("P28" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P28" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P28" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P28 = item.Element(cname).Value;
                                }
                                break;
                            case 29:
                                if ("P29" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P29" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P29" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P29 = item.Element(cname).Value;
                                }
                                break;
                            case 30:
                                if ("P30" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P30" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P30" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P30 = item.Element(cname).Value;
                                }
                                break;
                            case 31:
                                if ("P31" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P31" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P31" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P31 = item.Element(cname).Value;
                                }
                                break;
                            case 32:
                                if ("P32" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P32" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P32" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P32 = item.Element(cname).Value;
                                }
                                break;
                            case 33:
                                if ("P33" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P33" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P33" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P33 = item.Element(cname).Value;
                                }
                                break;
                            case 34:
                                if ("P34" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P34" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P34" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P34 = item.Element(cname).Value;
                                }
                                break;
                            case 35:
                                if ("P35" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P35" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P35" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P35 = item.Element(cname).Value;
                                }
                                break;
                            case 36:
                                if ("P36" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P36" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P36" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P36 = item.Element(cname).Value;
                                }
                                break;
                            case 37:
                                if ("P37" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P37" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P37" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P37 = item.Element(cname).Value;
                                }
                                break;
                            case 38:
                                if ("P38" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P38" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P38" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P38 = item.Element(cname).Value;
                                }
                                break;
                            case 39:
                                if ("P39" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P39" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P39" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P39 = item.Element(cname).Value;
                                }
                                break;
                            case 40:
                                if ("P40" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P40" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P40" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P40 = item.Element(cname).Value;
                                }
                                break;
                            case 41:
                                if ("P41" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P41" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P41" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P41 = item.Element(cname).Value;
                                }
                                break;
                            case 42:
                                if ("P42" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P42" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P42" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P42 = item.Element(cname).Value;
                                }
                                break;
                            case 43:
                                if ("P43" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P43" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P43" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P43 = item.Element(cname).Value;
                                }
                                break;
                            case 44:
                                if ("P44" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P44" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P44" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P44 = item.Element(cname).Value;
                                }
                                break;
                            case 45:
                                if ("P45" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P45" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P45" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P45 = item.Element(cname).Value;
                                }
                                break;
                            case 46:
                                if ("P46" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P46" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P46" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P46 = item.Element(cname).Value;
                                }
                                break;
                            case 47:
                                if ("P47" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P47" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P47" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P47 = item.Element(cname).Value;
                                }
                                break;
                            case 48:
                                if ("P48" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P48" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P48" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P48 = item.Element(cname).Value;
                                }
                                break;
                            case 49:
                                if ("P49" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P49" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P49" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P49 = item.Element(cname).Value;
                                }
                                break;
                            case 50:
                                if ("P50" == chartbidvalue)
                                {
                                    fp.P1000 = Convert.ToDouble(item.Element(cname).Value);
                                }
                                else if ("P50" == Imgbidvalue)
                                {
                                    fp.PBindingImgSource = GetImageStream(item.Element(cname).Value);
                                }
                                else
                                {
                                    if ("P50" == mapbidvalue)
                                    {
                                        fp.P2000 = item.Element(cname).Value;
                                    }
                                    fp.P50 = item.Element(cname).Value;
                                }
                                break;
                        }
                    }
                }
                itemlist.Add(fp);
            }
            if (apps.IsolatedStorageSavingSetting)
            {
                datatemp = XamlReader.Load(dss.GetModufiedTemplate(selectedfid)) as DataTemplate;
                BindData();
            }
            if (apps.WebServiceSavingSetting)
            {
                client.GetModufiedTemplateAsync(selectedfid);
                client.GetModufiedTemplateCompleted += new EventHandler<GetModufiedTemplateCompletedEventArgs>(client_GetModufiedTemplateCompleted);
            }
        }

        private ImageSource GetImageStream(string ImageName)
        {
            ImageSource imgsrc = dss.LoadFromLocalStorage(ImageName, "Images") as ImageSource;
            return imgsrc;
        }

        void client_GetModufiedTemplateCompleted(object sender, GetModufiedTemplateCompletedEventArgs e)
        {
            datatemp = XamlReader.Load(e.Result.ToString()) as DataTemplate;
            BindData();
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FormDataEntry.xaml?fid=" + selectedfid, UriKind.Relative));
        }

        void geocodeService_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
        {
            GeocodeResponse geocodeResponse = e.Result;
            double latitude = geocodeResponse.Results[0].Locations[0].Latitude;
            double longitude = geocodeResponse.Results[0].Locations[0].Longitude;
            Microsoft.Phone.Controls.Maps.Platform.Location Location = new Microsoft.Phone.Controls.Maps.Platform.Location();
            Location.Latitude = latitude;
            Location.Longitude = longitude;
            //var pp = new Microsoft.Maps.MapControl.Pushpin() { Location = new Microsoft.Maps.MapControl.Location(latitude, longitude) };
            Microsoft.Phone.Controls.Maps.Pushpin pp = new Microsoft.Phone.Controls.Maps.Pushpin() { Location = new System.Device.Location.GeoCoordinate(latitude, longitude) };


            pp.Content = i.ToString();
            pp.Foreground = new SolidColorBrush(Colors.Blue);
            pp.Background = new SolidColorBrush(Colors.Red);


            MyMap.Children.Add(pp);

            Microsoft.Phone.Controls.Maps.Platform.Location Maploc = new Microsoft.Phone.Controls.Maps.Platform.Location();
            Maploc.Latitude = latitude;
            Maploc.Longitude = longitude;
            MyMap.SetView(Maploc, 11);

            i = i + 1;
        }

        private readonly Microsoft.Phone.Controls.Maps.CredentialsProvider _credentialsProvider;

        public Microsoft.Phone.Controls.Maps.CredentialsProvider CredentialsProvider
        {
            get { return _credentialsProvider; }
        }

        void BindData()
        {
            if (datatemp != null)
            {
                if (LayoutType == "Map")
                {
                    MapaddData.ItemTemplate = datatemp;
                    GeocodeRequest geocodeRequest = new GeocodeRequest();
                    //Set the credentials using a valid Bing Maps key
                    RequestBase req = new RequestBase();
                    geocodeRequest.Credentials = new Credentials();
                    geocodeRequest.Credentials.ApplicationId = "Any8oa_tRDKqhncXvK3mEAKnUZXzONZsuu46NLOYee1nMoEsr_dwQ44VO62sV2Nd";
                    foreach (FieldProperties fpadd in itemlist)
                    {
                        // Set the full address query
                        geocodeRequest.Query = fpadd.P2000;
                        // Set the options to only return high confidence results
                        ConfidenceFilter[] filters = new ConfidenceFilter[1];
                        filters[0] = new ConfidenceFilter();
                        filters[0].MinimumConfidence = GeocodeService.Confidence.High;
                        GeocodeOptions geocodeOptions = new GeocodeOptions();
                        //geocodeOptions.Filters = filters;
                        geocodeRequest.Options = geocodeOptions;
                        GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
                        geocodeService.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted);
                        geocodeService.GeocodeAsync(geocodeRequest);
                    }
                    Microsoft.Phone.Controls.Maps.Credentials credentials = new Microsoft.Phone.Controls.Maps.Credentials();
                    credentials.ApplicationId = "AnJkQTP3TKWN8HrGCRBAOAFM-DhepezFQi3aTTCQMDIBEz_T65MguqW0mM7QG3Oe";
                    MyMap.CredentialsProvider = CredentialsProvider;
                    MyMap.Height = 250;
                    MyMap.Name = "MyMap";
                    MyMap.Mode = new Microsoft.Phone.Controls.Maps.AerialMode(true);
                    MapGrid.Children.Add(MyMap);
                    ChartGrid.Visibility = Visibility.Collapsed;
                    ContentGrid.Visibility = Visibility.Collapsed;
                    MapGrid.Visibility = Visibility.Visible;
                    MapaddData.ItemsSource = itemlist;
                }
                else if (LayoutType == "Chart")
                {
                    LineGraph lineGraph = new LineGraph();
                    lineGraph.ValueMemberPath = "P1000";
                    serialChart.Graphs.Add(lineGraph);
                    serialChart.Height = 400;
                    serialChart.AxisForeground = new SolidColorBrush(Colors.White);
                    serialChart.PlotAreaBackground = new SolidColorBrush(Colors.Black);
                    serialChart.GridStroke = new SolidColorBrush(Colors.DarkGray);
                    serialChart.CategoryValueMemberPath = chartcatbidvalue;
                    ChartGrid.Children.Add(serialChart);
                    ChartGrid.Visibility = Visibility.Visible;
                    ContentGrid.Visibility = Visibility.Collapsed;
                    MapGrid.Visibility = Visibility.Collapsed;
                    serialChart.DataSource = itemlist;
                }
                else
                {
                    lstData.ItemTemplate = datatemp;
                    ContentGrid.Visibility = Visibility.Visible;
                    ChartGrid.Visibility = Visibility.Collapsed;
                    MapGrid.Visibility = Visibility.Collapsed;
                    lstData.ItemsSource = itemlist;
                }
            }
        }
    }
}